package com.bysj.servlet;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class FileDownloadServlet extends HttpServlet {
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
				this.doPost(request, response);
			}
			public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
					response.setContentType("application/octet-stream");
					ServletContext application=this.getServletContext();
					String path=request.getParameter("name");
					path=new String(path.getBytes("ISO8859-1"));
					File file=new File(application.getRealPath("/docFile/temp")+File.separator+path);
					InputStream in=new FileInputStream(file);
					OutputStream os=response.getOutputStream();
					response.addHeader("Content-Disposition", "attachment;filename="+new String(file.getName().getBytes("GBK"),"UTF-8"));
		 			response.addHeader("Content-Length", new Long(file.length()).toString());
		 			System.out.println(new String(file.getName().getBytes("UTF-8"),"ISO8859-1").toString());
		 			response.setCharacterEncoding("UTF-8");
					int a=0;
					while((a=in.read())!=-1){
						os.write(a);
					}
					in.close();
					os.close();
			}
		}
